﻿using GRPCClass;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SdtpsLibrary.DAL
{
    public class SqliteDAL
    {
        public static int SaveTaskReplyErr(TaskReply vtaskrepl)
        {
            string tsql;
            //判断文件是否存在 
            string fullname = getfilename();
            SdtpsBase.SQLiteHelper mysql = new SdtpsBase.SQLiteHelper();
            if (!mysql.ExistsTable("taskreplyserr"))
            {
                tsql = createTable("taskreplyserr");
                mysql.ExecuteNonQuery(tsql);
            }
            string json = "";
            if (vtaskrepl.TaskInfo != null)
            {
                json = Newtonsoft.Json.JsonConvert.SerializeObject(vtaskrepl.TaskInfo);
            }
            tsql = string.Format("insert into taskreplyserr(id,status,message,NodeWorkID,ErrorMessage,TaskInfo)" +
              "select '{0}','{1}','{2}','{3}','{4}','{5}';"
              , vtaskrepl.ID, vtaskrepl.Status
              , vtaskrepl.Message == null ? "" : vtaskrepl.Message.Replace("'", "''")
              , vtaskrepl.NodeWorkID
              , vtaskrepl.ErrorMessage == null ? "" : vtaskrepl.ErrorMessage.Replace("'", "''")
              , json.Replace("'", "''"));
            return mysql.ExecuteNonQuery(tsql);
        }
        private static readonly object myobj = new object();
        /// <summary>
        /// 保存到本地表中
        /// </summary>
        /// <param name="vtaskrepl"></param>
        /// <returns></returns>
        public static int SaveTaskReply(TaskReply vtaskrepl)
        {
            string tsql;
            //判断文件是否存在 
            string fullname = getfilename();
            SdtpsBase.SQLiteHelper mysql = new SdtpsBase.SQLiteHelper();
            if (!mysql.ExistsTable("taskreplys"))
            {
                lock (myobj)
                {
                    tsql = createTable();
                    mysql.ExecuteNonQuery(tsql);
                }
            }
            string json = "";
            if (vtaskrepl.TaskInfo != null)
            {
                json = Newtonsoft.Json.JsonConvert.SerializeObject(vtaskrepl.TaskInfo);
            }
            tsql = string.Format("insert into taskreplys(id,status,message,NodeWorkID,ErrorMessage,TaskInfo)" +
              "select '{0}','{1}','{2}','{3}','{4}','{5}';"
              , vtaskrepl.ID, vtaskrepl.Status
              , vtaskrepl.Message == null ? "" : vtaskrepl.Message.Replace("'", "''")
              , vtaskrepl.NodeWorkID
              , vtaskrepl.ErrorMessage == null ? "" : vtaskrepl.ErrorMessage.Replace("'", "''")
              , json.Replace("'", "''"));
            return mysql.ExecuteNonQuery(tsql);


        }
        private static string createTable(string tabname= "taskreplys")
        { 
            string tsql =string.Format("create table {0}([objid] integer PRIMARY KEY autoincrement" +
                ",id varchar(50),status varchar(10),message text,NodeWorkID varchar(100),ErrorMessage text,TaskInfo text)", tabname);
            return tsql;
        }
        private static string getfilename()
        {
            string filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data");
            if (!System.IO.Directory.Exists(filePath))
            {
                System.IO.Directory.CreateDirectory(filePath);
            }
            string fullname = System.IO.Path.Combine(filePath, "sdtpstaskreply.db");
            if (!System.IO.File.Exists(fullname))
            {
                SdtpsBase.SQLiteHelper.CreateDB(fullname); 
            }

            SdtpsBase.SQLiteHelper.SetConnectionString(fullname, "s9d2t5p1st8a6s7k");

            return fullname;
        }

    }
}
